package com.pzhu.dao.impl;

import com.pzhu.dao.LoginDao;
import com.pzhu.pojo.User;

import java.sql.*;

public class LoginDaoImpl implements LoginDao {

    @Override
    public User checkUser(User userInPut) {
        //定义JDBC对象
        Connection conn = null;
        Statement stmt= null;
        ResultSet rs = null;
        //定义用户对象并初始化，存储查询结果
        User userSelect = null;
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //获取连接
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/jsp?&useSSL=false&useUnicode=true&&characterEncoding=utf8", "root", "0714");
            //创建SQL语句
            String sql = "select pwd from t_user where uname='"+userInPut.getUname()+"';";
            //获取Statement对象
            stmt = conn.createStatement();
            //执行SQL语句
            rs = stmt.executeQuery(sql);
            //遍历结果封装数据
            while (rs.next()) {
                userSelect = new User();
                userSelect.setPwd(rs.getString("pwd"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return userSelect;
    }
}
